package com.mkh.st.admin.feign;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mkh.st.admin.api.dto.*;
import com.mkh.st.admin.api.entity.SysUser;
import com.mkh.st.admin.api.feign.ISysUserProvider;
import com.mkh.st.admin.api.vo.DataPermissionVO;
import com.mkh.st.admin.api.vo.UserIdNameVO;
import com.mkh.st.admin.api.vo.UserPurchasePageVO;
import com.mkh.st.admin.service.SysUserService;
import com.mkh.st.common.core.constant.CommonConstants;
import com.mkh.st.common.core.constant.enums.ResultCodeEnum;
import com.mkh.st.common.core.util.R;
import com.mkh.st.common.security.annotation.Inner;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@AllArgsConstructor
public class SysUserProvider implements ISysUserProvider {

	private final SysUserService userService;

	@Override
	@Inner(value = false)
	@GetMapping("/user/provider/info/{phone}")
	public R<UserInfo> info(@PathVariable("phone") String phone) {
		SysUser user = userService.getOne(Wrappers.<SysUser>query().lambda()
				.eq(SysUser::getDelFlag, CommonConstants.NOT_DEL).eq(SysUser::getPhone, phone));
		if (user == null) {
			return R.failed(null, String.format("用户信息为空 %s", phone));
		}
		return R.ok(userService.findUserInfo(user));
	}

	@Override
	@GetMapping("/user/provider/social/info/{inStr}")
	public R<UserInfo> social(String inStr) {
		return null;
	}

	@Override
	@GetMapping("/user/provider/ancestor/{username}")
	public R<List<SysUser>> ancestorUsers(String username) {
		return null;
	}

	@Override
	@Inner(value = false)
	@PostMapping("/user/provider/lockUserStatus")
	public R<Boolean> lockUserStatus(@RequestBody UserLockDTO dto) {
		return R.ok(userService.lockUserStatus(dto));
	}

    @Override
	@GetMapping("/user/provider/getUserDataPermisstion")
    public R<DataPermissionVO> getUserDataPermisstion(@RequestParam(value = "docTypeCode",required = false)String docTypeCode,@RequestParam("menuCode") String menuCode) {
        return R.ok(userService.getUserDataPermisstion(docTypeCode,menuCode));
    }

	@Override
	@GetMapping("/user/provider/purchaseUserPage")
	public R<Page<UserPurchasePageVO>> purchaseUserPage(UserPurchasePageDTO dto) {
		return R.ok(userService.purchaseUserPage(dto));
	}

    @Override
	@GetMapping("/user/provider/purchaseUserList")
    public R<List<UserPurchasePageVO>> purchaseUserList(UserPurchasePageDTO dto) {
        return R.ok(userService.purchaseUserList(dto));
    }

	@Override
	@PostMapping("/user/provider/otherUserSave")
	public R<Long> otherUserSave(@RequestBody @Valid OtherUserDTO dto) {
//		return R.failed("qwer");
		return R.ok(userService.otherUserSave(dto));
	}

	@Override
	@PostMapping("/user/provider/deleteUser")
	public R<Boolean> deleteUser(@RequestBody @Valid DeleteDto dto) {
		return R.ok(userService.deleteUser(dto));
	}

	@Override
	public R<List<UserIdNameVO>> getUserNameList(String ids) {
		return R.ok(userService.getUserNameList(ids));
	}

	@Override
	public R<List<UserIdNameVO>> getSaleUserList(Integer businessType) {
		return R.ok(userService.getSaleUserList(businessType));
	}

	@Override
	public R<Boolean> checkUserPermission(String menuCode,Long scopeId, String name) {
		return R.ok(userService.checkUserPermission(menuCode,scopeId,name));
	}

	@Override
	public R<List<UserIdNameVO>> getUserAccountList(String menuCode) {
		return R.ok(userService.getAllUsersList(null,menuCode));
	}

    @Override
    public R<Boolean> addScopeData(@Valid ScopoDataDTO dto) {
        return R.ok(userService.addScopeData(dto));
    }

}
